package com.leiden.uni;

import android.graphics.BitmapFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import leiden.model.LeidenEnvPrinterModel;
import leiden.model.LeidenPrinterModel;
import leiden.model.LeidenSmallBitmapModel;

public class LeidenUtils {


    public static JSONObject toJSONObject(LeidenEnvPrinterModel leidenEnvPrinterModel) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("printerAccuracy",leidenEnvPrinterModel.getPrinterAccuracy());
        jsonObject.put("printfSpeed",leidenEnvPrinterModel.getPrintfSpeed());
        jsonObject.put("printfPotency",leidenEnvPrinterModel.getPrintfPotency());
        jsonObject.put("printfMedium",leidenEnvPrinterModel.getPrintfMedium());
        jsonObject.put("labelType",leidenEnvPrinterModel.getLabelType());
        jsonObject.put("topDeviation",leidenEnvPrinterModel.getTopDeviation());
        jsonObject.put("printfModel",leidenEnvPrinterModel.getPrintfModel());
        jsonObject.put("beStrippedModel",leidenEnvPrinterModel.getBeStrippedModel());
        jsonObject.put("beStrippedFeedVolume",leidenEnvPrinterModel.getBeStrippedFeedVolume());
        jsonObject.put("cutNumber",leidenEnvPrinterModel.getCutNumber());
        jsonObject.put("cutterFeedVolume",leidenEnvPrinterModel.getCutterFeedVolume());
        jsonObject.put("standardFeedVolume",leidenEnvPrinterModel.getStandardFeedVolume());

        return jsonObject;
    }

    public static LeidenPrinterModel createLeidenPrinterModel(JSONObject jsonObject) {

        LeidenPrinterModel leidenPrinterModel = new LeidenPrinterModel();

        leidenPrinterModel.setLabelW(jsonObject.getIntValue("labelW"));
        leidenPrinterModel.setLabelH(jsonObject.getIntValue("labelH"));

        Integer number = jsonObject.getInteger("number");
        if (number == null) {
            number = 1;
        }
        leidenPrinterModel.setNumber(number);

        if (!jsonObject.containsKey("bitmaps")) {
            return leidenPrinterModel;
        }

        JSONArray bitmaps = jsonObject.getJSONArray("bitmaps");

        for (int i = 0; i < bitmaps.size(); i++) {
            JSONObject bitmapJSONObject = bitmaps.getJSONObject(i);
            leidenPrinterModel.addAPLSmallBitmapModel(createLeidenSmallBitmapModel(bitmapJSONObject));
        }

        return leidenPrinterModel;
    }


    public static LeidenEnvPrinterModel createLeidenEnvPrinterModel(JSONObject jsonObject) {

        LeidenEnvPrinterModel leidenEnvPrinterModel = new LeidenEnvPrinterModel();

        Integer beStrippedFeedVolume = jsonObject.getInteger("beStrippedFeedVolume");
        if (beStrippedFeedVolume != null) {
            leidenEnvPrinterModel.setBeStrippedFeedVolume(beStrippedFeedVolume);
        }

        Integer cutNumber = jsonObject.getInteger("cutNumber");
        if (cutNumber != null) {
            leidenEnvPrinterModel.setCutNumber(cutNumber);
        }

        Integer cutterFeedVolume = jsonObject.getInteger("cutterFeedVolume");
        if (cutterFeedVolume != null) {
            leidenEnvPrinterModel.setCutterFeedVolume(cutterFeedVolume);
        }

        Integer standardFeedVolume = jsonObject.getInteger("standardFeedVolume");
        if (standardFeedVolume != null) {
            leidenEnvPrinterModel.setStandardFeedVolume(standardFeedVolume);
        }

        Integer printerAccuracy = jsonObject.getInteger("printerAccuracy");
        if (printerAccuracy != null) {
            leidenEnvPrinterModel.setPrinterAccuracy(printerAccuracy);
        }

        Integer printfSpeed = jsonObject.getInteger("printfSpeed");
        if (printfSpeed != null) {
            leidenEnvPrinterModel.setPrintfSpeed(printfSpeed);
        }

        Integer printfPotency = jsonObject.getInteger("printfPotency");
        if (printerAccuracy != null) {
            leidenEnvPrinterModel.setPrintfPotency(printfPotency);
        }

        Integer printfMedium = jsonObject.getInteger("printfMedium");
        if (printfMedium != null) {
            leidenEnvPrinterModel.setPrintfMedium(printfMedium);
        }

        Integer labelType = jsonObject.getInteger("labelType");
        if (labelType != null) {
            leidenEnvPrinterModel.setLabelType(labelType);
        }

        Integer topDeviation = jsonObject.getInteger("topDeviation");
        if (printerAccuracy != null) {
            leidenEnvPrinterModel.setTopDeviation(topDeviation);
        }

        Integer printfModel = jsonObject.getInteger("printfModel");
        if (printfModel != null) {
            leidenEnvPrinterModel.setPrintfModel(printfModel);
        }
        Integer beStrippedModel = jsonObject.getInteger("beStrippedModel");
        if (beStrippedModel != null) {
            leidenEnvPrinterModel.setBeStrippedModel(beStrippedModel);
        }

        return leidenEnvPrinterModel;

    }


    public static LeidenSmallBitmapModel createLeidenSmallBitmapModel(JSONObject jsonObject) {

        LeidenSmallBitmapModel leidenSmallBitmapModel = new LeidenSmallBitmapModel();

        Integer x = jsonObject.getInteger("x");
        if (x != null) {
            leidenSmallBitmapModel.setX(x);
        }

        Integer y = jsonObject.getInteger("y");
        if (y != null) {
            leidenSmallBitmapModel.setY(y);
        }

        Integer threshold = jsonObject.getInteger("threshold");
        if (threshold != null) {
            leidenSmallBitmapModel.setThreshold(threshold);
        }

        leidenSmallBitmapModel.setWD(jsonObject.getIntValue("WD"));
        leidenSmallBitmapModel.setHT(jsonObject.getIntValue("HT"));

        String bitmapPath = jsonObject.getString("bitmapPath");

        leidenSmallBitmapModel.setBitmap(BitmapFactory.decodeFile(bitmapPath));

        if (leidenSmallBitmapModel.getBitmap() == null) {
            throw new RuntimeException("创建bitmap失败");
        }

        return leidenSmallBitmapModel;
    }


}
